home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / CBASE.ZIP / CBDELCUR.C < prev    next >
Text File  |  1990-06-21  |  3KB  |  118 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbdelcur.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. /*#include <stdlib.h>*/
  10. /*#include <string.h>*/
  11.  
  12. /* local headers */
  13. #include "cbase_.h"
  14.  
  15. /*man---------------------------------------------------------------------------
  16. NAME
  17.      cbdelcur - delete current cbase record
  18.  
  19. SYNOPSIS
  20.      #include <cbase.h>
  21.  
  22.      int cbdelcur(cbp)
  23.      cbase_t *cbp;
  24.  
  25. DESCRIPTION
  26.      The cbdelcur function deletes the current record of cbase cbp.
  27.      The record cursor is set to the record following the deleted
  28.      record.  All key cursors are set to null.
  29.  
  30.      cbdelcur will fail if one or more of the following is true:
  31.  
  32.      [EINVAL]       cbp is not a valid cbase pointer.
  33.      [CBELOCK]      cbp is not write locked.
  34.      [CBENOPEN]     cbp is not open.
  35.      [CBENREC]      The record cursor of cbp is null.
  36.  
  37. SEE ALSO
  38.      cbinsert, cbrcursor.
  39.  
  40. DIAGNOSTICS
  41.      Upon successful completion, a value of 0 is returned.  Otherwise,
  42.      a value of -1 is returned, and errno set to indicate the error.
  43.  
  44. ------------------------------------------------------------------------------*/
  45. int cbdelcur(cbp)
  46. cbase_t * cbp;
  47. {
  48.     lspos_t lspos = NIL;
  49.     cbrpos_t cbrpos = NIL;
  50.     void *buf = NULL;
  51.     int i = 0;
  52.  
  53.     /* validate arguments */
  54.     if (!cb_valid(cbp)) {
  55.         errno = EINVAL;
  56.         return -1;
  57.     }
  58.  
  59.     /* check if not open */
  60.     if (!(cbp->flags & CBOPEN)) {
  61.         errno = CBENOPEN;
  62.         return -1;
  63.     }
  64.  
  65.     /* check if not write locked */
  66.     if (!(cbp->flags & CBWRLCK)) {
  67.         errno = CBELOCK;
  68.         return -1;
  69.     }
  70.  
  71.     /* check if record cursor is null */
  72.     if (lscursor(cbp->lsp) == NULL) {
  73.         errno = CBENREC;
  74.         return -1;
  75.     }
  76.  
  77.     /* get record position */
  78.     if (lsgetcur(cbp->lsp, &lspos) == -1) {
  79.         CBEPRINT;
  80.         return -1;
  81.     }
  82.     cbrpos = lspos;
  83.  
  84.     /* delete keys */
  85.     for (i = 0; i < cbp->fldc; ++i) {
  86.         if (cbp->fldv[i].flags & CB_FKEY) {
  87.             buf = calloc((size_t)1, cbp->fldv[i].len + sizeof(cbrpos_t));
  88.             if (buf == NULL) {
  89.                 CBEPRINT;
  90.                 errno = ENOMEM;
  91.                 return -1;
  92.             }
  93.             if (lsgetrf(cbp->lsp, cbp->fldv[i].offset, buf, cbp->fldv[i].len) == -1) {
  94.                 CBEPRINT;
  95.                 free(buf);
  96.                 return -1;
  97.             }
  98.             memcpy(((char *)buf + cbp->fldv[i].len), &cbrpos, sizeof(cbrpos_t));
  99.             if (btdelete(cbp->btpv[i], buf) == -1) {
  100.                 CBEPRINT;
  101.                 free(buf);
  102.                 return -1;
  103.             }
  104.             free(buf);
  105.             buf = NULL;
  106.         }
  107.     }
  108.  
  109.     /* delete current record */
  110.     if (lsdelcur(cbp->lsp) == -1) {
  111.         CBEPRINT;
  112.         return -1;
  113.     }
  114.  
  115.     errno = 0;
  116.     return 0;
  117. }
  118.